ggplot2: a system for declaratively creating graphics, based on “The Grammar of Graphics”ggplot2extensions.
Creating graphics with ggplot2
Marc Comas-Cufí
ggplot2: a system for declaratively creating graphics, based on “The Grammar of Graphics”ggplot2 extensions.ggplot2Fuel economy data from 1999 to 2008 for 38 popular models of cars:
# A tibble: 234 × 11
manufacturer model displ year cyl trans drv cty hwy fl
<chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr>
1 audi a4 1.8 1999 4 auto(l… f 18 29 p
2 audi a4 1.8 1999 4 manual… f 21 29 p
3 audi a4 2 2008 4 manual… f 20 31 p
4 audi a4 2 2008 4 auto(a… f 21 30 p
5 audi a4 2.8 1999 6 auto(l… f 16 26 p
# … with 229 more rows, and 1 more variable: class <chr>
ggplot2 ggplot(data = <DATA>) + # INITIAL LAYER
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>)) + # NEXT LAYER
⋮
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>)) + # LAST LAYER
<TUNNING>ggplot2displ \(\rightarrow\) x, hwy \(\rightarrow\) yggplot2displ \(\rightarrow\) x, hwy \(\rightarrow\) y, class \(\rightarrow\) colorggplot2displ \(\rightarrow\) x, hwy \(\rightarrow\) yggplot2displ \(\rightarrow\) x, hwy \(\rightarrow\) yggplot2displ \(\rightarrow\) x, hwy \(\rightarrow\) yggplot(data = mpg, aes(x = displ, y = hwy, color = class)) +
geom_point() +
labs(title = "Bivariate plot",
subtitle = "Relation between engine displacement and consumption",
x = 'Engine displacement (liters)', y = 'Consumption (milles x gallon)',
color = 'Car class', caption = "Statistical Programming Course")facet_wrap() and facet_grid())facet_wrap() and facet_grid())ggplot2figureIf plot parameter is omitted, last plot will be saved.
ggplot2gggplot2: Elegant Graphics for Data Analysis https://ggplot2-book.org/origin?dtab = flights %>% count(origin) %>%
mutate(p = sprintf("%0.1f%%", 100*prop.table(n)),
cn = rev(cumsum(rev(n))),
y = cn + diff(c(cn,0))/2 )
ggplot(data=flights) +
geom_bar(aes(x="",y=(..count..), fill=origin)) +
geom_text(data=dtab, aes(x="", y=y, label = p)) +
coord_polar(theta = 'y', start = pi/2, direction = 1) +
theme_void()dep_delay?dep_delay?dep_delay?dep_delay?dep_delay?dep_delay?dep_delay and origin?dep_delay and origin?dep_delay and origin?dep_delay and origin?origin and on.timeorigin relative frequenciesarrival relative frequenciesdep_delay and arr_delay?ggplot2 extensionsggthemes. https://github.com/jrnold/ggthemesggtech. https://github.com/ricardo-bion/ggtechggthemr: https://github.com/cttobin/ggthemrcowplot: https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.htmlbbplot. https://bbc.github.io/rcookbookhrbrthemes: https://github.com/hrbrmstr/hrbrthemesplotlygganimatelibrary(gganimate)
library(gapminder)
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent) +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')gganimaterayshader